package cn.com.b2office.struts2.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import cn.com.b2office.domain.Category;
import cn.com.b2office.domain.Item;
import cn.com.b2office.domain.ItemBrand;
import cn.com.b2office.service.CategoryService;
import cn.com.b2office.service.ItemBrandService;
import cn.com.b2office.service.ItemCommentService;
import cn.com.b2office.service.ItemRankService;
import cn.com.b2office.service.ItemService;

import com.opensymphony.xwork2.ActionSupport;

public class ItemViewAction extends ActionSupport implements
		ServletRequestAware, ServletResponseAware {

	private static final long serialVersionUID = -7771041711555066477L;

	// 存放最近浏览的cookie名称
	public static final String COOKIE_NAME = "historyitems";

	// 保存最近浏览的数目
	private static final int HISTORY_SIZE = 10;

	private HttpServletRequest request = null;

	private HttpServletResponse response = null;

	private String itemId = null;

	private Item item = null;

	private ItemService itemService = null;

	private ItemBrandService itemBrandService = null;

	private CategoryService categoryService = null;

	private ItemRankService itemRankService = null;

	private ItemCommentService itemCommentService = null;

	private ItemBrand brand = null;

	private List<Item> history = null;

	private List<Category> ancestries = null;// 父类

	private Category category = null;// 当前商品分类

	private List<Item> rankItems = null;// 当前商品所在三级分类的商品排行

	private List<Item> topCategoryRankItems = null;// 当前商品所在的顶层分类的商品排行

	private int cmmSize = 0;// 商品评论条数

	public String execute() throws Exception {
		// 最近浏览
		Cookie c = getCookie();
		List<String> historyItemIdList = getHistoryRecords(c);
		history = itemService.findItemsByIterate(historyItemIdList);
		historyItemIdList.add(itemId);
		updateHistoryRecord(c, historyItemIdList, itemId);

		item = itemService.getItem(itemId);
		brand = itemBrandService.getItemBrand(item.getItem_brand());
		ancestries = categoryService.findAncestries(item.getItem_clsno());
		category = categoryService.getCategory(item.getItem_clsno());

		rankItems = itemRankService.getRank(item.getItem_clsno(), 6);
		topCategoryRankItems = itemRankService.getRank(item.getItem_clsno()
				.substring(0, 2), 6);
		cmmSize = itemCommentService.getCommentSize(itemId);
		return SUCCESS;

	}

	/**
	 * 获取最近浏览历史记录的cookie中
	 * 
	 * @return
	 */
	private Cookie getCookie() {
		Cookie[] cookies = request.getCookies();
		if (cookies == null || cookies.length == 0) {
			return null;
		}
		for (Cookie c : cookies) {
			if (c.getName().equals(COOKIE_NAME)) {
				return c;
			}
		}
		return null;
	}

	/**
	 * 获取最近浏览记录
	 * 
	 * @return
	 */
	private List<String> getHistoryRecords(Cookie c) {
		List<String> list = new ArrayList<String>();
		if (c == null || c.getValue() == null || c.getValue().trim().equals(""))
			return list;
		String[] values = c.getValue().split("&");
		for (int i = 0; i < values.length; i++) {
			list.add(values[i]);
		}
		return list;

	}

	private void updateHistoryRecord(Cookie c, List<String> records,
			String newItemId) {

		List<String> newList = new ArrayList<String>();
		newList.add(newItemId);
		for (int i = 0; i < records.size(); i++) {
			String itemId = records.get(i);
			if (!itemId.equals(newItemId))
				newList.add(records.get(i));
		}
		records = newList;

		// 如果大于指定长度，则剪裁去
		if (records.size() > HISTORY_SIZE)
			records = records.subList(records.size() - HISTORY_SIZE, records
					.size());

		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < records.size() - 1; i++) {
			sb.append(records.get(i)).append("&");
		}
		sb.append(records.get(records.size() - 1));
		String value = sb.toString();
		if (c == null) {
			c = new Cookie(COOKIE_NAME, value);
			// 设置cookie生存周期为关闭浏览器时
			c.setMaxAge(-1);
			response.addCookie(c);
			return;
		}
		c.setValue(value);
		response.addCookie(c);
		return;

	}

	public void setItemService(ItemService itemService) {
		this.itemService = itemService;
	}

	public void setItemBrandService(ItemBrandService itemBrandService) {
		this.itemBrandService = itemBrandService;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public void setItemRankService(ItemRankService itemRankService) {
		this.itemRankService = itemRankService;
	}

	public void setItemCommentService(ItemCommentService itemCommentService) {
		this.itemCommentService = itemCommentService;
	}

	public void setItemId(String itemId) {
		this.itemId = itemId;
	}

	public Item getItem() {
		return item;
	}

	public ItemBrand getBrand() {
		return brand;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;

	}

	public List<Item> getHistory() {
		return history;
	}

	public List<Category> getAncestries() {
		return ancestries;
	}

	public Category getCategory() {
		return category;
	}

	public List<Item> getRankItems() {
		return rankItems;
	}

	public List<Item> getTopCategoryRankItems() {
		return topCategoryRankItems;
	}

	public int getCmmSize() {
		return cmmSize;
	}

}
